Ext.define('CA.catalog.ObjectWindow',{
    extend: 'Ext.Window',
    xtype: 'CA.catalog.ObjectWindow',

    uses: [
        'CA.base.ImagesPanel',

        'CA.catalog.ObjectForm',
        'CA.catalog.ObjectProperties'
    ],

    maximizable: true,
    width: '80%',
    height: '80%',
    layout: 'fit',
    border: false,

    initComponent: function(){
        var config = {
            title: 'Новый объект',
            iconCls: 'c-icon-add',
            modal: true,
            items: [
                {
                    xtype: 'tabpanel',
                    layout: 'fit',
                    resizeTabs: true,
                    enableTabScroll: true,
                    items: [
                        {
                            xtype: 'CA.catalog.ObjectForm',
                            itemId: 'form',
                            layout: 'fit',
                            iconCls: 'c-icon-preferences'
                        },
                        {
                            title: 'Содержание',
                            iconCls: 'c-icon-html',
                            itemId: 'html',
                            layout: 'fit',
                            xtype: 'htmleditor',
                            cls: 'c-no-border'
                        },
                        {
                            title: 'Дополнительно',
                            xtype: 'CA.catalog.ObjectProperties',
                            layout: 'fit'
                        },
                        {
                            title: 'Карта',
                            iconCls: 'c-icon-map'
                        },
                        {
                            xtype: 'CA.base.ImagesPanel',
                            title: 'Фотогаллерея',
                            iconCls: 'c-icon-images',
                            cls: 'c-catalog-photos',
                            urlFilter: 'catalog/' + this.record.internalId,
                            layout: 'fit',
                            hidden: this.openType == 'create'
                        }
                    ]
                }
            ],
            buttons: [
                {
                    text: 'Принять',
                    iconCls: 'c-icon-success',
                    handler: this.doApply,
                    scope: this
                },
                {
                    text: 'Отмена',
                    iconCls: 'c-icon-cancel',
                    handler: this.destroy,
                    scope: this
                }
            ]
        };

        if(this.openType != 'create'){
            config.title = 'Объект: ' + this.record.get('name');
            config.iconCls = 'c-icon-house';
            config.modal = false;
        }

        Ext.apply(this, config);

        this.callParent();
        this.on('afterrender', this.onAterRender, this);
    },

    onAterRender: function(){
        var form = this.down('#form').getForm(),
            html = this.down('#html'),
            data = this.record.data;

        html.setValue(data.html);
        form.setValues(data);
    },

    doApply: function(){
        var form = this.down('#form').getForm(),
            html = this.down('#html').getValue(),
            vals = form.getValues(),
            record = this.record;

        vals.html = html;

        if(form.isValid()){
            record.set(vals);

            this.fireEvent('apply', record);
            this.close();
        }
    }
});